<?php
/*
 * Glorious Framework
 *
 * WordPress POMO-based Language manager
 *
 * (c) 2007-2010 Konspire Design
 *
 * $Id: language.php,v 17989674e79b 2010/05/07 08:10:08 and $
 * $Author: and $
 * $Date: 2010/05/07 08:10:08 $
 */

require_once (dirname(__FILE__) . '/pomo/mo.php');
require_once (dirname(__FILE__) . '/pomo/po.php');


class Language extends Singleton
{

    protected $l10n_path = 'language';
    protected $l10n_locale = 'ru_RU';
    protected $l10n = array();

    static public function &instance() {
        return parent::instance(__CLASS__);
    }

    protected function __construct()
    {
        $conf_l10n_path = config::instance()->locale__translations_path;
        $conf_l10n_locale = config::instance()->locale__lang;

        if(!empty($conf_l10n_path))
            $this->l10n_path = $conf_l10n_path;

        if(!empty($conf_l10n_locale))
            $this->l10n_locale = $conf_l10n_locale;

        $this->l10n_path = rtrim($this->l10n_path, '/') . '/';

        // Log::instance()->write('L10n Class initialized');

        $this->load_default_textdomain();
    }

    public function get_locale() {
        return $this->l10n_locale;
    }

    public function load_default_textdomain() {
		foreach( array_reverse( rad_base_paths() ) as $path ) {
	        $this->load_textdomain('default', $path . $this->l10n_path . '/' );
		}
    }

    public function load_textdomain($domain, $mopath)
    {
		$locale_guesses = $this->get_locale_variants($this->l10n_locale);
		$mofile = fetch_file($mopath . $domain . '/', $locale_guesses, '', 'mo');

        // Log::instance()->write('L10n Loading "%s" textdomain', $domain);

        if(!$mofile)
			return false;

        $mo = new MO();
        if(!$mo->import_from_file($mofile))
            return false;

        if(isset($this->l10n[$domain])) {
            $this->l10n[$domain]->merge_with($mo);
		} else {
	        $this->l10n[$domain] = &$mo;
		}

        return true;
    }

    public function &get_translations_for_domain( $domain ) {
        if(!isset( $this->l10n[$domain])) {
            $this->l10n[$domain] = new NOOP_Translations;
        }

        return $this->l10n[$domain];
    }

    public function translate($text, $domain = 'default') {
        $translations = &$this->get_translations_for_domain($domain);

        return $translations->translate($text);
    }

    public function _x( $single, $context, $domain = 'default' ) {
		return translate_with_gettext_context( $single, $context, $domain );
	}

    public function translate_plural($single, $plural, $number, $domain = 'default') {
        $translations = &$this->get_translations_for_domain( $domain );
        $translation = $translations->translate_plural($single, $plural, $number);

        return $translation;
    }

    public function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
        $translations = &get_translations_for_domain($domain);
        
        return $translations->translate($text, $context);
    }

    public function translate_plural_with_context($single, $plural, $number, $context, $domain = 'default') {
        $translations = &$this->get_translations_for_domain( $domain );
        $translation = $translations->translate_plural( $single, $plural, $number, $context );

        return $translation;
    }

	public function get_locale_variants($locale) {

		$guesses = array( );
		
		$language = substr($locale,0,2);
		if(empty($language))
			$language = $locale;

		$dialect = (strpos($locale,'_')!==false) ? substr($locale,strpos($locale,'_')+1,2) : strtoupper($language);
		$charset = (strpos($locale,'.')!==false) ? substr($locale,strpos($locale,'.')+1) : null;

		$guesses[] = $locale;

		if(!is_null($charset))
			$guesses[] = $language . '_' . $dialect . '.' . $charset;

		$guesses[] = $language . '_' . $dialect;
		$guesses[] = $language;

		$guesses = array_unique($guesses);

		return $guesses;
	}

}

/* End of file language.php */
/* Location: ./system/libraries/language.php */